<?php

class AdminLoginView extends RegularView
{
	private $oModel;
	
	public function __construct(AdminLoginModel $model)
	{
		$this->oModel = $model;
	}
	
	protected function title() { return TextManager::i()->text("PAGE_TITLE_ADMIN_LOGIN", false); }
	
	protected function name() { return "adminlogin"; }
	
	protected function body()
	{
		$login = null;
		$redirect = null;
		$patterns = array
		(
			"need" => "NEED_LOGIN",
		);
		if (Input::hasRequest("pattern") && isset($patterns[Input::request("pattern")]))
		{
		?>
			<div><?php TextManager::i()->text($patterns[Input::request("pattern")]); ?></div>
		<?php
		}
		
		if (Input::hasRequest("redirect"))
			$redirect = Input::request("redirect");
		if (Input::hasRequest("login"))
			$login = Input::request("login");
		else if (Input::hasRequest("name"))
			$login = Input::request("name");
		
		$errors = array();
		if ($this->oModel instanceof AdminLoginModel)
			$errors = $this->oModel->errors();
		
		$this->loginForm($login, $errors, $redirect);
	} 
	
	private function loginForm($login, $errors, $redirect)
	{
	?>
	<form action = "<?php $this->url(array("action" => "login")); ?>" method = "POST">
		<?php if (!is_null($redirect)): ?>
			<input type = "hidden" name = "redirect" value = "<?php echo $redirect; ?>" />
		<?php endif; ?>
		<table class = "form_table">
			<tr<?php if ($regAction && isset($errors["name"])): ?> class = "error" <?php endif; ?>><td><?php TextManager::i()->text("TITLE_ADMIN_LOGIN"); ?></td><td><input type = "text" name = "name" value = "<?php if (!is_null($login)) echo htmlspecialchars($login); ?>" /></td></tr>
			<tr<?php if ($regAction && isset($errors["password"])): ?> class = "error" <?php endif; ?>><td><?php TextManager::i()->text("TITLE_PASSWORD"); ?></td><td><input type = "password" name = "password" /></td></tr>
			<tr><td colspan = "2" class = "btn"><input type = "submit" value = "<?php TextManager::i()->text("LOGIN"); ?>" /></td></tr>
			<?php foreach ($errors as $error): ?>
				<tr><td class = "error" colspan = "2">*<?php TextManager::i()->text($error); ?></td></tr>
			<?php endforeach; ?>
		</table>
	</form>
	<?php
	}
}

?>